package com.xiguaji.tiktok.task.blogger.event;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.gugee.tiktok.common.model.dal.blogger.BloggerMainRepository;
import com.gugee.tiktok.common.model.dto.SlimBloggerDto;
import com.gugee.tiktok.common.model.mysql.BloggerMain;
import com.xiguaji.boot.eventbus.Event;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.eventbus.IEventListener;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class RefreshCustomBloggerMainEventListener implements IEventListener<RefreshCustomBloggerMainEventListener.RefreshCustomBloggerMainEvent> {

    @Autowired
    BloggerMainRepository bloggerMainRepository;
    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;
    @PostConstruct
    public void initConstruct(){
    }

    @Override
    @Subscribe
    @AllowConcurrentEvents
    public void handle(RefreshCustomBloggerMainEventListener.RefreshCustomBloggerMainEvent event) { ;
        long startTime = System.currentTimeMillis();
        Thread single= new Thread(new RefreshCustomBloggerMainEventListener.SingleTask(this,event));
        single.start();
        try {
            single.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            XxlJobLogger.log(e);
        }
        XxlJobLogger.log("RefreshCustomBloggerMainEvent END   use time:{}s", (System.currentTimeMillis()-startTime)/1000);

    }


    static class SingleTask implements Runnable{
        private RefreshCustomBloggerMainEventListener topHandler;
        private RefreshCustomBloggerMainEventListener.RefreshCustomBloggerMainEvent event;
        public SingleTask(RefreshCustomBloggerMainEventListener topHandler, RefreshCustomBloggerMainEventListener.RefreshCustomBloggerMainEvent event){
            this.topHandler=topHandler;
            this.event=event;
        }
        @Override
        public void run() {
            topHandler.run(event);

        }
    }
    public void run(RefreshCustomBloggerMainEventListener.RefreshCustomBloggerMainEvent event) {

        List<BloggerMain> bloggerMains = bloggerMainRepository.findByoUniqueIdIn(event.getUniqueIds());
        List<SlimBloggerDto> bloggers = new ArrayList<>();

        bloggerMains.forEach( bloggerMain -> {
                SlimBloggerDto slimBloggerDto = new SlimBloggerDto();
                slimBloggerDto.setUid(bloggerMain.getOUid());
                slimBloggerDto.setRegion(bloggerMain.getORegion());
                slimBloggerDto.setId(bloggerMain.getId());
                bloggers.add(slimBloggerDto);
            });
        long start = System.currentTimeMillis();

        if (bloggers.size() > 0){
            var saveBloggerEvent = new UpdateBloggerMainEventListener.UpdateBloggerMainEvent(event, bloggers,true);
            eventBusTemplate.post(saveBloggerEvent);
        }
        log.info("RefreshCustomBloggerMainEvent END dataSize:{},user time:{}s",bloggers.size(),(System.currentTimeMillis()-start)/1000);
        XxlJobLogger.log("RefreshCustomBloggerMainEvent END dataSize:{},user time:{}s",bloggers.size(),(System.currentTimeMillis()-start)/1000);



    }
    public static class RefreshCustomBloggerMainEvent extends Event {
        private List<String> uniqueIds=new ArrayList<>(16);

        public List<String> getUniqueIds() {
            return uniqueIds;
        }


        public RefreshCustomBloggerMainEvent(String params) {
            super("RefreshCustomBloggerMainEvent");
            String[] ps;
            if(params!=null){
                ps=params.split(",");
                if(ps.length>0) {
                    uniqueIds= Arrays.asList(ps);
                }
            }


        }
    }
}